<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="取消"
    wrapClassName="ant-modal-cust-warp"
    style="top:5%;height: 100%;overflow-y: hidden">
    <template slot="footer">
      <a-button key="back" v-if="isReadOnly" @click="handleCancel">
        取消
      </a-button>
    </template>
    <a-spin :spinning="confirmLoading">
    <a-form :form="form">
      <a-form-item label="工程名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select placeholder="选择工程名称" v-decorator.trim="[ 'engineerId', validatorRules.engineerId]"  :dropdownMatchSelectWidth="false"
           showSearch optionFilterProp="children" @change="changeSelect" >
            <a-select-option v-for="(item,index) in engineerList" :key="index" :value="item.id">
              {{ item.name }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="纳税人信息" :labelCol="labelCol" :wrapperCol="wrapperCol" >
          <a-input  v-decorator.trim="['nameA']"  :read-only="true"/>
        </a-form-item>
        <a-form-item label="工程创建时间" :labelCol="labelCol" :wrapperCol="wrapperCol" >
          <j-date  v-decorator="['engineerTime', validatorRules.engineerTime]" :read-only="true"/>
        </a-form-item>
         <a-form-item label="合同金额" :labelCol="labelCol" :wrapperCol="wrapperCol" >
          <a-input  v-decorator.trim="[ 'engineerMoney', validatorRules.engineerMoney]" suffix="万元" :read-only="true" />
        </a-form-item>
        <a-form-item label="已开票金额" :labelCol="labelCol" :wrapperCol="wrapperCol" >
          <a-input  v-decorator.trim="['amounted', validatorRules.amounted]" suffix="万元" :read-only="true"/>
        </a-form-item>
        <a-form-item label="开票金额" :labelCol="labelCol" :wrapperCol="wrapperCol" >
          <a-input placeholder="请输入开票金额" v-decorator.trim="[ 'amount', validatorRules.amount]" suffix="万元"/>
        </a-form-item>
         <a-form-item label="开票批次" :labelCol="labelCol" :wrapperCol="wrapperCol" >
          <a-input placeholder="请输入开票批次" v-decorator.trim="[ 'batch', validatorRules.batch]" :read-only="true" />
        </a-form-item>

        <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea :rows="3" placeholder="请输入备注信息" v-decorator="[ 'description' ]" />
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="上传发票">
          <j-image-upload v-model="fileList" bizPath="receipt" text="上传" isMultiple></j-image-upload>
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>
<script>
  import pick from 'lodash.pick'
  import Vue from 'vue'
  import JSelectPosition from '@/components/jeecgbiz/JSelectPosition'
  import { ACCESS_TOKEN } from "@/store/mutation-types"
  import {addReceipt,editReceipt} from '@/api/api'
  import JDate from '@/components/jeecg/JDate'
  import JUpload from '@/components/jeecg/JUpload'
  import JImageUpload from '@/components/jeecg/JImageUpload'
  import {ReceiptListMixin } from '../mixins/ReceiptListMixin'
  export default {
    name: "ReceiptModal",
    mixins:[ReceiptListMixin],
    components: {
      JImageUpload,
      JUpload,
      JDate,
      JSelectPosition
    },
    data () {
      return {
        title:"操作",
        visible: false,
        modalWidth:800,
        drawerWidth:700,
        fileList:[],
        engineerTimeStr:'',
        model: {},
        id:"", //保存工程id
        isReadOnly: false,
        disableSubmit:false,
        dateFormat:"YYYY-MM-DD",
        validatorRules:{
          engineerName:{
            rules: [{
              required: true, message: '请输入工程项目名称!'
            }]
          },
          engineerTime:{
            rules: [{
              required: true, message: '请输入工程创建时间!'
            }]
          },
          amount:{
            rules: [{
              required: true,type:'number', message: '请输入开票金额!', trigger: 'blur',
               transform (value) {
                  var moneyg = /^\d+$|^\d*\.\d+$/g
                  if (!moneyg.test(value) ) {
                    return false
                  }else{
                    return Number(value)
                  }
                }
            }]
          }
        },
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        uploadLoading:false,
        confirmLoading: false,
        headers:{},
        form:this.$form.createForm(this)
      }
    },
    created () {
      const token = Vue.ls.get(ACCESS_TOKEN);
      this.headers = {"X-Access-Token":token}
    },
    methods: {
     add () {
        this.edit({});
      },
      edit (record) {
      
        this.form.resetFields();
        this.id = record.id;
        this.visible = true;
        this.model = Object.assign({}, record);
        this.editAfter(this.model);
      },
       //调用完edit()方法之后会自动调用此方法
      editAfter() {
        if (this.action === 'add') {
          this.fileList = []
        } else {
           setTimeout(() => {
            this.fileList = this.model.fileName
          }, 5)
          this.$nextTick(() => {
            this.form.setFieldsValue(pick(this.model,'engineerId','nameA','engineerTime','engineerMoney','amounted','amount','batch','description'))
          });
        }
         let params = {
            type: 'receipt',
            engineerId:this.model.engineerId
          }
         this.initEngineer(params)
      },
     
      close() {
        this.visible = false;
        this.$emit('close');
        this.disableSubmit = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let formData = Object.assign(this.model, values);
            // let formData = this.classifyIntoFormData(values)
            if(this.fileList && this.fileList.length > 0) {
             formData.fileName = this.fileList
           } else {
             formData.fileName = ''
            }
            let obj;
            if(!this.model.id){
              formData.id = this.id;
              obj=addReceipt(formData);
            }else{
              obj=editReceipt(formData);
            }
            obj.then((res)=>{
              if(res.code === 200){
                that.$emit('ok');
              }else{
                that.$message.warning(res.data.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })

          }
        })
      },
      handleCancel() {
        this.close()
      },
      changeSelect(value) {
          for (var i = 0; i <this.engineerList.length; i++) {
           var oneSubject=this.engineerList[i]
            if(value===oneSubject.id){
              this.produceList = oneSubject.produceName
              this.form.setFields({ engineerTime : {value:oneSubject.time}})
              this.form.setFields({ engineerMoney : {value:oneSubject.money}});
              this.form.setFields({ amounted : {value:oneSubject.amounted}});
              this.form.setFields({ batch : {value:oneSubject.batch}});
              this.form.setFields({ nameA : {value:oneSubject.nameA}});
            }
          }
      }
    }
  }
</script>

<style scoped>

</style>